package com.sq.partner.manager.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sq.partner.manager.common.Constants;
import com.sq.partner.manager.common.exception.AppException;
import com.sq.partner.manager.mybatis.model.CoreDept;
import com.sq.partner.manager.mybatis.model.GatewayApp;
import com.sq.partner.manager.mybatis.model.GatewayChannelInfo;
import com.sq.partner.manager.mybatis.model.GatewayTrans;
import com.sq.partner.manager.mybatis.model.GatewayUser;
import com.sq.partner.manager.mybatis.model.TreeDto;
import com.sq.partner.manager.service.IGatewayAppService;
import com.sq.partner.manager.service.IGatewayChannelInfoConfigService;
import com.sq.partner.manager.service.IGatewayDataManageService;
import com.sq.partner.manager.service.IGatewayTransManagerService;
import com.sq.partner.manager.service.IGatewayTxLogService;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.StrUtil;

/**
 * 
 * @author duguangwei
 * 交易分析
 */
@Controller
@RequestMapping(value = "/transManager")
public class TransManagerController {

	@Resource
	private  IGatewayTransManagerService gatewayTransManagerService;
	@Resource
	private IGatewayChannelInfoConfigService channelInfoConfigService;
	@Resource
	private  IGatewayTxLogService gatewayTxLogService;
	@Resource
	private IGatewayAppService appService;
	@Resource
	private IGatewayDataManageService dataManageService;
	
	@RequestMapping(value = "/gatewayTXLogAnalysis")
	public String gatewayTXLogAnalysis() throws Exception {
		return "transManager/gatewayTXLogAnalysis";
	}
	@RequestMapping("/queryPrivilegeBranch")
	@ResponseBody
    public List<CoreDept> queryAppList(HttpSession session) throws Exception {
		GatewayUser user=(GatewayUser) session.getAttribute(Constants.SESSION_USER);
		if(null!=user && StrUtil.isNotEmpty(user.getDepartmentStr())){
			return dataManageService.queryDeptList(user);
		}
		return null;
    }
	@RequestMapping("/queryList")
	public @ResponseBody Page<GatewayTrans> queryList(String channel,String trxDate,String trxCode,String deptCode,String mark,Integer size,Integer current,String subPartnerNo , String applicationCode,HttpSession session) throws Exception {
		Page<GatewayTrans> page=new Page<GatewayTrans>();
		if(StrUtil.isEmpty(deptCode)){
			page.setResult("error");
			page.setMessage("机构字段不能为空");
			return page;
		}else if(!(channelInfoConfigService.checkIsHasPrivilege(session, deptCode))){
			page.setMessage("权限不足，无权限使用交易明细查询!");
			page.setResult("error");
			return page;
		}
		page.setCurrent(current);
		page.setSize(size);
		//GatewayUser gatewayuser=(GatewayUser) session.getAttribute(Constants.SESSION_USER);
		return gatewayTransManagerService.querypage(page,channel,trxDate,trxCode,deptCode,mark,subPartnerNo,applicationCode );
	}
	
	/**
	 * 根据Appid查询与之对应的ChannelInfo
	 * @param fkAppid
	 * @return List<GatewayChannelInfo>
	 * @throws Exception 
	 */
	@RequestMapping("/loadChannelInfoByAppid")
	public @ResponseBody Page<GatewayChannelInfo> loadChannelInfoByAppid(Integer size,Integer pageIndex,String deptCode,String channelName,String channelCode) throws Exception{
		Page<GatewayChannelInfo> page = new Page<GatewayChannelInfo>();
		if(StrUtil.isEmpty(deptCode)){
			page.setResult("error");
			page.setMessage("查询失败，机构字段不能为空！");
			return page;
		}
		page.setCurrent(pageIndex);
		page.setSize(size);
		GatewayChannelInfo channelInfo = new GatewayChannelInfo();
		channelInfo.setDeptCode(deptCode);
		channelInfo.setChannelCode(channelCode);
		channelInfo.setChannelName(channelName);
		page.setType(channelInfo);
		return channelInfoConfigService.getChannelInfoListByDeptCode(page);
	}
	/**
	 * 根据渠道ID 查询出渠道的交易名称
	 * 
	 */
	@RequestMapping("/loadTransNameBychannelCode")
	public @ResponseBody Page loadTransNameBychannelCode(String channel_Code,String appid,String application) throws Exception{
		Page page=new Page();
		/*if(StrUtil.isEmpty(channel_Code)){
			page.setResult("error");
			page.setMessage("合作伙伴代码不能为空");
			return page;
		}*/
		page   = channelInfoConfigService.getTranstypeBychannelCode(channel_Code,appid,application);
		return page;
	}
	/**
	 * 根据 查询出渠道的应用名称
	 * 
	 */
	@RequestMapping("/loadTransNameByApplication")
	public @ResponseBody Page loadTransNameByApplication(String appid,String channel_Code) throws Exception{
		Page page=new Page();
	/*	if(StrUtil.isEmpty(channel_Code)){
			page.setResult("error");
			page.setMessage("合作伙伴代码不能为空");
			return page;
		}*/
		page   =  channelInfoConfigService.loadTransNameByApplication1(appid);
		return page;
	}
	
	
	
	/**
	 * 查询交易明细相关合作伙伴
	 * @param fkAppid
	 * @return List<GatewayChannelInfo>
	 * @throws Exception 
	 */
	@RequestMapping(value = "/querySubChannelTree",method = RequestMethod.POST)
	@ResponseBody
	public List<TreeDto> querySubChannelTree(String parentChannelCode) throws Exception{
		if(StrUtil.isEmpty(parentChannelCode)){
			throw new AppException("合作伙伴代码不能为空");
		}
		
		return appService.querySubChannelTreeByParentChannelCode(parentChannelCode);
	}
	/**
	 * 查询所有的应用信息
	 * @return
	 */
	@RequestMapping(value = "/loadApplicationData",method = RequestMethod.POST)
	@ResponseBody
	public List<GatewayApp> loadApplicationData() throws Exception{
		
		return appService.loadApplicationData();
	}
	
	
}
